﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WFL.Office.Word
{
    public class WordRange
    {
        private readonly object _lock = new object();

        public string Text
        {
            get { return _range.Text; }
            set { _range.Text = value; }
        }

        public WordFont Font
        {
            get { return new WordFont(_range.Font); }
            set { _range.Font = value._font; }
        }

        public int Start
        {
            get { return _range.Start; }
        }

        public int End
        {
            get { return _range.End; }
        }

        public byte[] EnhMetaFileBits
        {
            get { return (byte[])_range.EnhMetaFileBits; }
        }

        private WordParagraphs _paragraphs;
        public WordParagraphs Paragraphs
        {
            get
            {
                if (_paragraphs == null)
                {
                    lock (_lock) { if (_paragraphs == null) { _paragraphs = new WordParagraphs(_range.Paragraphs); } }
                }
                return _paragraphs;
            }
        }

        private WordTables _tables;
        public WordTables Tables
        {
            get
            {
                if (_tables == null)
                {
                    lock (_lock) { if (_tables == null) { _tables = new WordTables(_range.Tables) { Document = Document }; } }
                }
                return _tables;
            }
        }

        private WordBookmarks _bookmarks;
        public WordBookmarks Bookmarks
        {
            get
            {
                if (_bookmarks == null)
                {
                    lock (_lock) { if (_bookmarks == null) { _bookmarks = new WordBookmarks(_range.Bookmarks) { Document = Document }; } }
                }
                return _bookmarks;
            }
        }

        private WordHyperlinks _hyperlinks;
        public WordHyperlinks Hyperlinks
        {
            get
            {
                if (_hyperlinks == null)
                {
                    lock (_lock) { if (_hyperlinks == null) { _hyperlinks = new WordHyperlinks(_range.Hyperlinks) { Document = Document }; } }
                }
                return _hyperlinks;
            }
        }

        private WordFields _fields;
        public WordFields Fields
        {
            get
            {
                if (_fields == null)
                {
                    lock (_lock) { if (_fields == null) { _fields = new WordFields(_range.Fields); } }
                }
                return _fields;
            }
        }

        private WordShapeRange _shapeRange;
        public WordShapeRange ShapeRange
        {
            get
            {
                if (_shapeRange == null)
                {
                    lock (_lock) { if (_shapeRange == null) { _shapeRange = new WordShapeRange(_range.ShapeRange); } }
                }
                return _shapeRange;
            }
        }

        private WordInlineShapes _inlineShapes;
        public WordInlineShapes InlineShapes
        {
            get
            {
                if (_inlineShapes == null)
                {
                    lock (_lock) { if (_inlineShapes == null) { _inlineShapes = new WordInlineShapes(_range.InlineShapes) { Document = Document }; } }
                }
                return _inlineShapes;
            }
        }

        private WordListFormat _listFormat;
        public WordListFormat ListFormat
        {
            get
            {
                if (_listFormat == null)
                {
                    lock (_lock) { if (_listFormat == null) { _listFormat = new WordListFormat(_range.ListFormat); } }
                }
                return _listFormat;
            }
        }

        internal Range _range
        {
            get;
            private set;
        }

        public WordDocument Document
        {
            get;
            internal set;
        }

        internal WordRange(Range range)
        {
            _range = range;
        }

        public void InsertBefore(string text)
        {
            _range.InsertBefore(text);
        }

        public void InsertAfter(string text)
        {
            _range.InsertAfter(text);
        }

        public void InsertParagraphAfter()
        {
            _range.InsertParagraphAfter();
        }

        public void InsertFile(string fileName, object range = null, bool confirmConversions = false, bool link = false, bool attachment = false)
        {
            object rangeArg = range ?? Type.Missing;
            _range.InsertFile(fileName, rangeArg, confirmConversions, link, attachment);
        }

        public void SetRange(int start, int end)
        {
            _range.SetRange(start, end);
        }

        public object GetStyle()
        {
            object obj = _range.get_Style();
            if (obj is Style)
            {
                return new WordStyle((Style)obj);
            }
            else
            {
                return obj;
            }
        }

        /// <summary>
        /// 设置样式
        /// </summary>
        /// <param name="style">样式名称、样式索引、BuiltinStyle、WordStyle</param>
        public void SetStyle(object style)
        {
            if (style is WordStyle)
            {
                _range.set_Style(((WordStyle)style)._style);
            }
            else
            {
                _range.set_Style(style);
            }
        }

        public void PasteAndFormat(RecoveryType type)
        {
            _range.PasteAndFormat((WdRecoveryType)type);
        }

        public void Collapse(CollapseDirection direction = CollapseDirection.wdCollapseStart)
        {
            _range.Collapse(direction);
        }

        public int Expand(Units unit = Units.wdCharacter)
        {
            return _range.Expand(unit);
        }
    }
}
